import { defineStore } from 'pinia'
import {toRaw,toRefs,markRaw,isProxy} from 'vue'
import {cloneDeep, isArray} from 'lodash';
interface InputItem {
  ref: string;
  name: string;
  category: string;
  value: string;
  description: string;
  requestParams?: any;
  responseParams?: any;
}
interface Template {
  id: string;
  name: string;
  type: string;
  data: {
    title: string;
    name: string;
    props: {
      description: string;
    };
    input: InputItem[];
    output:any[];
  };
}

const template:any = {
  name: '插件节点',
  type: "plugin",
  data: {
    "title": "插件节点",
    "name": "",
    "props": {
      'description': '请提供需要添加到提示词中的信息，这些信息可以被提示词引用。'
    },
    "input": [
      // {
      //   "ref": "",
      //   "name": "name",
      //   "category": "1",
      //   "value": "outputText",
      //   'description': '', //tooltip描述
      // }, {
      //   "name": "entity_type",
      //   "category": "2",
      //   "value": "1",
      //   'description': '',
      // }, {
      //   "name": "page_num",
      //   "category": "2",
      //   "value": "1",
      //   'description': '',
      // }, {
      //   "name": "page_size",
      //   "category": "2",
      //   "value": "1",
      //   'description': '',
      // }, {
      //   "name": "keyword",
      //   "category": "2",
      //   "value": "必应",
      //   'description': '',
      // }
    ],
    "output":[]
  }
}

export const usePluginStore = defineStore({
  id: 'usePluginStore',
  state: () : { nodes: any,ids:string[] }=> ({
    nodes:[],
    ids:[],
  }),
  actions: {
    init(data:any){
      if(data){
        this.nodes=[]
        data.forEach((el:any) => {
          el.data.input.forEach((item:any)=>{
            if(item.category=='1'){
              item.Options = [{
                label:item.value,
                value:item.value
              }]}
             return item
          })
        });
        this.nodes = [...toRaw(this.nodes),...data]
        this.ids.push(data.id)
      }else{
      }
    },
    setId(id:string){
      this.ids.push(id)
      this.nodes.length && (this.nodes[this.nodes.length - 1].id = id);
    },
    // initTemplate(data:any){
    //   const {id} = data;
    //   this.nodes[this.nodes.length - 1].id = id
    // },
    addPluginNode(data:any){
      const targetTemp = {
        name: '插件节点',
        type: "plugin",
        data: {
          "title": "插件节点",
          "name": "",
          "props": {
            'description': '请提供需要添加到提示词中的信息，这些信息可以被提示词引用。'
          },
          "input": [],
          "output":[],
          "outputDisplay":[]
        }
      }
      const {name:title,requestParams=[],responseParams=[],id} = data;
      targetTemp.data.title =title;
      const newTemplate = targetTemp;
      isArray(requestParams)&&requestParams.map((item:any)=>{
        newTemplate.data.input.push({
          ref:'',
          "category": "1",
          value:item.desc,
          description:item.desc,
          name:item.name,
          // requestParams,
        })

      })
      newTemplate.data.name = id;
      const outputdisplayTemp = isProxy(responseParams)? toRaw(responseParams):responseParams||[]
      this.nodes=[...this.nodes,newTemplate]
      // newTemplate.data.outputDisplay = outputdisplayTemp;
      newTemplate.data.output =[{
        name:'output',
        type:'5',
        value:[{
          name:'output',
          subParams:outputdisplayTemp
        }]
      }]
      console.log(newTemplate,'api-----node')
    },
    updateRef(id:string){
      this.nodes.forEach((el:any) => {
        el.data.input.forEach((item:any)=>{
          item.ref = id
        })
      });
      // this.nodes = toRefs([...nodes])
    },
    clearData(id:string){
      this.nodes.forEach((el:any) => {
        el.data.input.forEach((item:any)=>{
          // if(item.ref == id){
            item.value = ''
          // }
        })
      });
    }
  }
})
